package com.yjlove.myplay.ui.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.yjlove.myplay.R;
import com.yjlove.myplay.bean.ShowImageInfo;
import com.yjlove.myplay.utils.ImagPagerUtil;
import com.yjlove.myplay.utils.ImageFormatUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者 YJlvoe
 * 创建时间 2017/2/23.
 */
public class ImagesAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    private Activity mActivity;
    private List<ShowImageInfo> mImagesInfoList = new ArrayList<>();
    private List<String> mImagesPathList = new ArrayList<>();

    public ImagesAdapter(Activity mActivity, List<ShowImageInfo> imagesInfoList)
    {
        mInflater = LayoutInflater.from(mActivity);
        this.mActivity = mActivity;
        this.mImagesInfoList = imagesInfoList;
        for (ShowImageInfo info : imagesInfoList){
            mImagesPathList.add(info.getImagePath() + info.getImageName());
        }
    }

    @Override
    public int getCount()
    {
        return mImagesInfoList.size();
    }

    @Override
    public ShowImageInfo getItem(int position)
    {
        return mImagesInfoList.get(position);
    }

    @Override
    public long getItemId(int position)
    {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.images_gridview_item, parent,
                    false);
            viewHolder = new ViewHolder();
            viewHolder.mImageView = (ImageView) convertView
                    .findViewById(R.id.image);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        ImageLoader.getInstance().displayImage("file://" + mImagesInfoList.get(position).getImagePath() + mImagesInfoList.get(position).getImageName(), viewHolder.mImageView, ImageFormatUtil.getOptions());
        viewHolder.mImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ImagPagerUtil imagPagerUtil = new ImagPagerUtil(mActivity, mImagesPathList, position);
                imagPagerUtil.setContentText(mImagesInfoList.get(position).getImageName());
                imagPagerUtil.show();
            }
        });
        return convertView;
    }

    private final class ViewHolder {
        ImageView mImageView;
    }
}
